package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.core.domain.entity.SysUser;
import com.znjc.common.enums.BusinessType;
import com.znjc.device.domain.DeviceCommand;
import com.znjc.device.domain.DeviceCommandRef;
import com.znjc.device.domain.DeviceType;
import com.znjc.device.service.IDeviceCommandRefService;
import com.znjc.device.service.IDeviceCommandService;
import com.znjc.device.service.IDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 *
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/commoandRef")
public class DeviceCommandRefController extends BaseController {
    @Autowired
    private IDeviceCommandRefService deviceCommandRefService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private IDeviceCommandService deviceCommandService;


    @PreAuthorize("@ss.hasPermi('device:type:edit')")
    @GetMapping("/authCommand/{typeId}")
    public AjaxResult authDataCheck(@PathVariable("typeId") Long typeId) {
        AjaxResult ajax = AjaxResult.success();
        DeviceType type = deviceTypeService.getById(typeId);
        ajax.put("type", type);
        List<DeviceCommand> commands = deviceCommandService.list();
        if (CollectionUtils.isNotEmpty(commands)) {
            List<Long> dataCommandIds = deviceCommandRefService.getIdList(typeId);
            if (CollectionUtils.isNotEmpty(dataCommandIds)) {
                commands.stream().map(d -> {
                    if (dataCommandIds.contains(d.getId())) {
                        d.setFlag(true);
                    }
                    return d;
                }).collect(Collectors.toList());
            }
            ajax.put("commands", SysUser.isAdmin(typeId) ? commands : commands.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        }
        return ajax;
    }

    /**
     * 设备类别绑定命令
     */
    @PreAuthorize("@ss.hasPermi('device:type:edit')")
    @Log(title = "设备类别绑定命令", businessType = BusinessType.GRANT)
    @PutMapping("/authCommand")
    public AjaxResult insertAuthRole(Long typeId, Long[] dataCommandIds) {
        if (typeId != null && dataCommandIds.length == 0) {
            LambdaQueryWrapper<DeviceCommandRef> wrapper = new LambdaQueryWrapper<DeviceCommandRef>();
            wrapper.eq(DeviceCommandRef::getTypeId, typeId);
            deviceCommandRefService.remove(wrapper);
        }
        List<DeviceCommandRef> refList = new ArrayList<DeviceCommandRef>();
        if (typeId != null && dataCommandIds.length > 0) {
            DeviceCommandRef ref = null;
            for (int i = 0; i < dataCommandIds.length; i++) {
                ref = new DeviceCommandRef();
                ref.setCommandId(dataCommandIds[i]);
                ref.setTypeId(typeId);
                refList.add(ref);
            }
        }
        if (CollectionUtils.isNotEmpty(refList)) {
            //先删除
            LambdaQueryWrapper<DeviceCommandRef> wrapper = new LambdaQueryWrapper<DeviceCommandRef>();
            wrapper.eq(DeviceCommandRef::getTypeId, typeId);
            deviceCommandRefService.remove(wrapper);
            //再保存
            deviceCommandRefService.saveBatch(refList);
        }
        return success();
    }

}
